package com.example.socket.util;

import com.example.socket.ServletConstant;
import com.example.socket.model.MulitiFile;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Classname HttpFormDataParser
 * @Description TODO
 * @Date 2023/4/27 12:02
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class HttpFormDataParser {

    private static String patternString = "Content-Disposition:\\s*form-data;\\s*name=\"(?<name>.*?)\"(?:;\\s*filename=\"(?<filename>.*?)\")?(?:\\r\\nContent-Type:\\s*(?<type>.*?))?\\r\\n\\r\\n(?<value>[\\s\\S]*?)\\r\\n(?=--)";

    private static String patternFormDataString="(?<=\\r\\n\\r\\n)[\\s\\S]*";

    private static Pattern pattern = Pattern.compile(patternString, Pattern.MULTILINE);

    private static Pattern patternFormData=Pattern.compile(patternFormDataString);

    public static Map<String,Object> getFormData(String content) throws Exception {
        Matcher matcher = pattern.matcher(content);
        Map<String, Object> formData = new HashMap<>();
        while (matcher.find()) {
            String name = matcher.group("name");
            String filename = matcher.group("filename");
            String type = matcher.group("type");
            String value = matcher.group("value");
            if (filename != null) {
                formData.put(name + ".filename", filename);
                System.out.println(filename);
                if (type != null) {
                    formData.put(name + ".type", type);
                    System.out.println(type);
                }
                byte[] bytes=value.getBytes(ServletConstant.BYTE_CODE);
                filename=new String (filename.getBytes (ServletConstant.BYTE_CODE),ServletConstant.STRING_CODE);
                MulitiFile mulitiFile=new MulitiFile(filename,bytes,type);
                formData.put(name,mulitiFile);
            }else {
                value=new String (value.getBytes (ServletConstant.BYTE_CODE),ServletConstant.STRING_CODE);
                formData.put(name, value.trim());
            }
        }
        return formData;
    }


    public static Map<String,Object> getFormDataStr(String content,String boundary) throws UnsupportedEncodingException {
        Matcher matcher = patternFormData.matcher(content);
        String formData="";
        if (matcher.find()) {
            formData = matcher.group(0);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        String[] parts = formData.split(boundary);
        for (String part : parts) {
            if (part.contains("filename=")) {
                String str = part.substring(part.indexOf("filename=\"") + "filename=\"".length(), part.indexOf("\r\n\r\n"));
                String fileName=str.substring(0,str.indexOf("\""));
                fileName=new String (fileName.getBytes (ServletConstant.BYTE_CODE),ServletConstant.STRING_CODE);
                String fileType=str.substring(str.indexOf("Content-Type: ")+"Content-Type: ".length());
                byte[] fileData = part.substring(part.indexOf("\r\n\r\n") + "\r\n\r\n".length(), part.lastIndexOf("\r\n")).getBytes("ISO-8859-1");
                MulitiFile mulitiFile=new MulitiFile(fileName,fileData,fileType);
                params.put(fileName,mulitiFile);
            } else if(part.contains("name=")) {
                String paramName = part.substring(part.indexOf("name=\"") + "name=\"".length(), part.lastIndexOf("\""));
                paramName=new String(paramName.getBytes(ServletConstant.BYTE_CODE),ServletConstant.STRING_CODE);
                String paramValue = part.substring(part.indexOf("\r\n\r\n") + "\r\n\r\n".length(), part.lastIndexOf("\r\n"));
                paramValue=new String(paramValue.getBytes(ServletConstant.BYTE_CODE),ServletConstant.STRING_CODE);
                params.put(paramName,paramValue);
            }
        }
        return params;
    }


}
